﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DepartmentSystem
{
    public partial class WorkerInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSoure();
            }
            else
            {
                GridviewControl.ResetGridView(this.GridView1);
            }
        }

        public void BindSoure()
        {
            // this.GridView1.DataSource = SQLHelper.executeReader("select workerNum,workerName,age,wage,deptNum,phoneNum,office from tb_worker");
            // this.GridView1.DataBind();
            GridviewControl.GridViewDataBind(this.GridView1,
                SQLHelper.GetTable("select workerNum,workerName,age,wage,deptNum,phoneNum,office from tb_worker"));
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //获得编辑列
            GridView1.EditIndex = e.NewEditIndex;
            //重新绑定数据
            BindSoure();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //退出编辑状态
            GridView1.EditIndex = -1;
            //重新绑定数据
            BindSoure();
        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int workerNum = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string sql = "delete from tb_worker where workerNum='" + workerNum + "'";


            if (SQLHelper.executeNoQuery(sql) != 1)
            {
                Response.Write("<script>alert('删除失败！')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除成功！')</script>");
            }
            //重新绑定数据
            BindSoure();
        }

        protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {
            int workNum = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string workerName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
            string strAge = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
            string strWage = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
            string deptNum = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
            string phoneNum = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
            string office = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();

            try
            {
                if (strAge.Length != 0)
                {
                    int age = Int16.Parse(strAge);
                    if (age < 0 || age > 150)
                    {
                        Response.Write("<script>alert('年龄范围 0 - 150!')</script>"); return;
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('年龄无效!')</script>"); return;
            }

            try
            {
                if (strWage.Length != 0)
                {
                    int wage = Int16.Parse(strWage);
                    if (wage < 800 || wage > 4500)
                    {
                        Response.Write("<script>alert('工资范围 800 - 4500!')</script>");
                        return;
                    }
                }
            }
            catch (Exception)
            {
                Response.Write("<script>alert('工资无效!')</script>"); return;
            }

            string sql = "update tb_worker set workerName='" + workerName + "', " + "Age='" + strAge + "', " + "Wage='" + strWage + "', " + "deptNum='"
                + deptNum + "', " + "phoneNum='" + phoneNum + "', " + "office='" + office + "' where workerNum='" + workNum + "'";


            if (SQLHelper.executeNoQuery(sql) != 1)
            {
                Response.Write("<script>alert('更新失败！')</script>");
            }
            else
            {
                Response.Write("<script>alert('更新成功！')</script>");
            }
            //退出编辑状态
            GridView1.EditIndex = -1;
            //重新绑定数据
            BindSoure();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Server.Transfer("DepartmentInfo.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Server.Transfer("WorkerInfo.aspx");
        }

        protected void Button1_Click1(object sender, EventArgs e)
        { 
            string workerNum = TextBox1.Text;
            string workerName = TextBox2.Text;

            string sql = "select * from tb_worker where 1 = 1";

            if (workerNum.Length != 0)
            {
                sql += " and workerNum = '" + workerNum + "'";
            }

            if (workerName.Length != 0)
            {
                sql += " and workerName like '%" + workerName + "%'";
            }

            //this.GridView1.DataSource = SQLHelper.executeReader(sql);
            //this.GridView1.DataBind();
            GridviewControl.GridViewDataBind(this.GridView1, SQLHelper.GetTable(sql));
        }
    }
}